package view.room;

import common.RoomState;
import common.RoomTypes;
import model.room.Room;
import view.MyAbstractTableModel;
import view.login.Login;

import java.util.ArrayList;

/**
 * @author mxd
 * @date 2024/5/26 0026
 */
//房间信息
public  class RoomTableModel extends MyAbstractTableModel<Room>  {

    public RoomTableModel() {
        //初始化数据
        datas = new ArrayList<Room>();
        datas = Room.getRooms();

        //定义表头
        this.columnNames = new String[]{"房间号","价格","房间状态", "房间类型"};
    }


    @Override
    protected Object getProperty(Object obj, int columnIndex) {
        Room c = (Room) obj;
        Object result = "";
        switch (columnIndex) {
            case 0:
                result = c.getRoomNumber();
                break;
            case 1:
                result = c.getPrice();
                break;
            case 2: {
                switch (c.getRoomState()) {
                    case EMPTY:
                        result = "空闲";
                        break;
                    case BOOKED:
                        result = "预订";
                        break;
                    case CHECKIN:
                        result = "入住";
                        break;
                    case REPAIR:
                        result = "维修";
                        break;
                }

                break;
            }
            case 3: {
                switch (c.getRoomTypes()) {
                    case Single_ROOM:
                        result = "单人间";
                        break;
                    case Double_ROOM:
                        result = "大床房";
                        break;
                    case Deluxe_ROOM:
                        result = "豪华间";
                        break;
                    case Twin_ROOM:
                        result = "双人间";
                        break;
                }
            }
            break;
        }
        return result;
    }

    // 增加房间
/*    public boolean addRoom(Room room) {
        if (room != null||!room.getRoomNumber().equals(datas.contains(room.getRoomNumber()))) {
            datas.add(room);
            fireTableDataChanged();
            return true;
        }
        return false;
    }*/
    // 增加房间
    public boolean addRoom(Room room) {
        for (Room existingRoom : datas) {
            if (existingRoom.getRoomNumber().equals(room.getRoomNumber())) {
                return false; // 如果找到相同房间号的房间，直接返回false
            }
        }
        datas.add(room); // 没有找到相同房间号的房间，添加新房间
        fireTableDataChanged();
        return true;
    }



    // 更新房间状态
    public void updateRoom(String roomNumber, RoomState roomState) {
        for (Room room : datas) {
            if (room.getRoomNumber().equals(roomNumber)) {
                room.setRoomState(roomState);
                break;
            }
        }

        /* for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getRoomNumber().equals(roomNumber)) {
                datas.get(i).setRoomState(roomState);
                break;
            }
        }*/

    }


    // 删除房间
    public void deleteRoom(String roomNumber) {
        for (int i = 0; i < datas.size(); i++) {
            Room room = (Room) datas.get(i);
            if (room.getRoomNumber() == roomNumber) {
                datas.remove(i);
                break;
            }
        }


    }
}